package exercise10;

public class Sort {

    public void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    public void shallSort (int[] array) {
        int gap = array.length / 2;
        while (gap > 0) {
            shall(array,gap);
            gap /= 2;
        }
    }

    public void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length-1;
        while (end >= 0) {
            swap(array, 0, end);
            end--;
            siftDown(array,0,end);
        }
    }

    public void mergeSort (int[] array) {
        mergeFun(array,0,array.length-1);
    }

    private void mergeFun(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        int midIndex = start + (end - start)  / 2;

        mergeFun(array,start,midIndex);
        mergeFun(array,midIndex+1,end);

        merge(array,start,end,midIndex);
    }

    private void merge(int[] array, int start, int end, int midIndex) {
        int rs = midIndex + 1;
        int[] tmpArray = new int[end-start+1];
        int k = 0;
        int tmpIndex = start;
        while (start <= midIndex && rs <= end) {
            if (array[start] < array[rs]) {
                tmpArray[k++] = array[start++];
            } else {
                tmpArray[k++] = array[rs++];
            }
        }
        while (start <= midIndex) {
            tmpArray[k++] = array[start++];
        }
        while (rs <= end) {
            tmpArray[k++] = array[rs++];
        }
        for (int j = tmpIndex; j <= end; j++) {
            array[j] = tmpArray[j - tmpIndex];
        }
    }

    private void createBigHeap(int[] array) {
        for (int parent = (array.length-2)/2; parent >= 0; parent--) {
            siftDown(array,parent,array.length-1);
        }
    }

    private void siftDown(int[] array, int parent, int end) {
        int child = 2 * parent + 1;
        while (child <= end) {
            if (child + 1 <= end && array[child+1] > array[child]) {
                child++;
            }
            if (array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = 2 * child + 1;
            } else {
                break;
            }
        }
    }

    private void shall(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int j = i - gap;
            int tmp = array[i];
            for (; j >= 0; j-=gap) {
                if (array[j] >= tmp) {
                    array[j+gap] = array[j];
                } else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    private void quick(int[] array, int start, int end) {
        if (start >= end) return;

        int part = partition2(array,start,end);

        quick(array,start,part-1);
        quick(array,part+1,end);

    }

    private int partition2(int[] array, int left ,int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    private int partition(int[] array, int left, int right) {
        int tmp = array[left];
        int tmpIndex = left;
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,tmpIndex);
        return left;
    }

    private void swap(int[] array, int x, int y) {
        int tmp = array[x];
        array[x] = array[y];
        array[y] = tmp;
    }
}
